package com.ynet.middleground.user.job;

import com.google.common.base.Throwables;
import com.ifp.framework.ClusterJob;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.model.CloudPlatformAsync;
import com.ynet.middleground.user.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @program: 2021-09-29
 * @description: 同步云平台定时任务
 * @author: 宋奕江
 * @create: 2021-09-29
 **/
@Component
public class CloudPlatformSyncJob extends ClusterJob {

    @Autowired
    CloudPlatformAsync cloudPlatformAsync;

    // 每一分钟跑一次 测试的时候用
    // @Scheduled(cron = "0 */2 * * * ? ")
    // 每天两点跑批
    @Scheduled(cron = "0 0 2 * * ? ")
    public void cloudPlatformSync() throws Exception {
        // 是否领导者
        if (!isLeader()) {
            IfpLogger.info("job", "程序不执行");
            return;
        }

        // 中台失败数据同步到云平台
        doCloudPlatformSync();
    }

    // 每天二十二点点跑批
    @Scheduled(cron = "0 0 22 * * ? ")
    // @Scheduled(cron = "0 */1 * * * ? ")
    public void GetCloudPlatformId() throws Exception {
        // 是否领导者
        if (!isLeader()) {
            IfpLogger.info("job", "程序不执行");
            return;
        }

        // 跑批拉取云平台id
        try {
            IfpLogger.info("job", "执行获取云平台id跑批开始 - " + LocalDate.now());
            cloudPlatformAsync.getCloudPlatformId();
            IfpLogger.info("job", "执行获取云平台id跑批结束 - " + LocalDate.now());
        } catch (BusinessException be) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(be));
            return;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
        }
    }

    /**
     * 中台失败数据同步到云平台
     */
    private void doCloudPlatformSync() {

        IfpLogger.info("job", "执行中台失败记录数据同步到云平台更新开始");
        cloudPlatformAsync.doCloudPlatformSync();
        IfpLogger.info("job", "执行中台失败记录数据同步到云平台更新结束");

    }
}
